//
//  VideoPlayInfo.swift
//  Motion
//
//  Created by 悦月越悦 on 2021/3/21.
//

import SwiftUI

struct VideoPlayInfo: View {
    //用户计次的时间显示
    @State var timerForType1Action: CustomerTimer? = nil
    //用于计时运动的时间显示
    @State var timerForType2Action: CustomerTimer = CustomerTimer(interval: 1,count: 0,type: .Add)
    
    @Binding var orientation: Orientation
    //是否打开提示的开关
    @EnvironmentObject var videoPlayModel: VideoPlayModel
    
    var body: some View {
        if videoPlayModel.curAction != nil {
            VStack{
                HStack{
                    Spacer()
                    CountTime()
                }.padding(.horizontal,18)
                
                Spacer()
                
                HStack{
                    if let action = videoPlayModel.curAction,action.type == 1{
                        VStack{
                            Text("\(timerForType1Action?.count ?? 0)/\(action.total)")
                                .customFont(28, true)
                            
                            Text(action.actionName)
                                .customFont(28, true)
                        }
                        .onChange(of: videoPlayModel.aVideoDuration, perform: { value in
                            print("videoPlayModel.curIndex=\(videoPlayModel.curIndex)")
                            print("videoPlayModel.aVideoDuration=\(value)")
                            if(videoPlayModel.curIndex != 0 && (timerForType1Action?.timer != nil)){
                                timerForType1Action!.cancel()
                            }
                            timerForType1Action =  CustomerTimer(interval: value/Double(videoPlayModel.curAction!.count),count: 0,type: .Add)
                            timerForType1Action!.startTimer()
                            
                        })
                    }
                    
                    if let action = videoPlayModel.curAction,action.type == 2{
                        Text("\(getTimeString(Double(timerForType2Action.count)))/\(getTimeString(videoPlayModel.totalDuration))")
                            .customFont(28, true)
                            .onChange(of: videoPlayModel.aVideoDuration, perform: { value in
                                timerForType2Action.cancel()
                                timerForType2Action.startTimer()
                            })
                    }
                    
                    Spacer()
                }.padding(.horizontal,18)
                
                if videoPlayModel.tipBtn && (videoPlayModel.nAction != nil){
                    VStack(){
                        Spacer()
                        if(videoPlayModel.curAction!.restDuration > 0){
                            Text("休息一下♨️")
                        }else{
                            Text("下一个动作:\(videoPlayModel.nAction?.actionName ?? "")")
                                .customFont(20, true)
                                .foregroundColor(.white)
                        }
                        Spacer()
                    }
                    .frame(width:UIScreen.main.bounds.width,height:50)
                    .background(Color("lightPurple"))
                }
                
                ProgressView(value: videoPlayModel.per, total: 1)
                    .progressViewStyle(MyProgressViewStyle(foregroundColor: Color("topicPurple"), backgroundColor: Color("topicPurple").opacity(0.5)))
                    .frame(height: orientation == .portrait ? 100 : 20)
            }
            .onDisappear(perform: {
                if(videoPlayModel.complete){
                    cancelTimer(true,true)
                }
                
                if(videoPlayModel.restPresent){
                    cancelTimer(true,false)
                }
                
            })
        }
    }
    
    //参数是秒数，获得 00:00的时间格式
    func getTimeString(_ seconds:Double) -> String {
        let m = Int(seconds/60)
        let s = Int(seconds.truncatingRemainder(dividingBy: 60))
        return String(format: "%d:%02d", arguments: [m, s])
    }
    
    func cancelTimer(_ firstCancel:Bool,_ secondCancel:Bool){
        if(firstCancel){
            if(timerForType1Action != nil){
                timerForType1Action?.cancel()
            }
        }
        
        if(secondCancel){
            if(timerForType2Action != nil){
                timerForType2Action.cancel()
            }
        }
    }
    
    
}
